package com.njupt.yanglonglong.report.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njupt.yanglonglong.report.demo.common.Result;
import com.njupt.yanglonglong.report.demo.dto.ExperimentInfoDto;
import com.njupt.yanglonglong.report.demo.entity.RAppliedExperiment;
import com.njupt.yanglonglong.report.demo.entity.RUser;
import com.njupt.yanglonglong.report.demo.service.RAppliedExperimentService;
import com.njupt.yanglonglong.report.demo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @className: AppliedExperimentController
 * @Description:
 * @author: lpl
 * @date: 2021/5/5 14:09
 */
@RestController
@Slf4j
@CrossOrigin
public class AppliedExperimentController {

    @Autowired
    RAppliedExperimentService rAppliedExperimentService;

    @Autowired
    RedisUtil redisUtil;


    /**
     * 请求已选实验列表，teacherId,experId
     *
     * @param request teacherId
     * @param map     courseId
     * @return List<ExperimentInfoDto>
     */
    @RequestMapping("/queryTeacherExperiment")
    public Result<JSONObject> queryTeacherExperiment(HttpServletRequest request, @RequestBody HashMap<String, String> map) {
        String token = request.getHeader("Authorization");
        RUser user = redisUtil.getUser(token);
        log.info("/queryTeacherExperiment接到请求,user:" + user + "data:" + map);
        map.put("teacherId", user.getUserId());

        JSONObject res = new JSONObject();
        List<ExperimentInfoDto> experimentInfoDtos = rAppliedExperimentService.queryAppliedExperimentList(map);
        if (experimentInfoDtos == null) {
            log.info("查询到的数据为:" + experimentInfoDtos);
            res.put("msg", "没有查询到您选择的实验");
            return Result.success(res);
        }
        log.info("queryTeacherExperiment成功查询到并返回 data:" + experimentInfoDtos);
        res.put("data", experimentInfoDtos);
        return Result.success(res);
    }

    /**
     * 选择实验,在数据库中添加数据，需要参数experId、startTime、endTime(userId)
     *
     * @param request experId、startTime、endTime
     * @param map     userId
     * @return isSuccess
     */
    @RequestMapping("/chooseExperiment")
    public Result<JSONObject> chooseExperiment(HttpServletRequest request, @RequestBody HashMap<String, String> map) {
        String token = request.getHeader("Authorization");
        RUser user = redisUtil.getUser(token);
        log.info("/chooseExperiment接到请求,user:" + user + " map：" + map);

        RAppliedExperiment rAppliedExperiment = new RAppliedExperiment();
        rAppliedExperiment.setTeacherId(user.getUserId());
        rAppliedExperiment.setExperId(map.get("experId"));
        rAppliedExperiment.setStartTime(map.get("startTime"));
        rAppliedExperiment.setEndTime(map.get("endTime"));
        rAppliedExperiment.setState(1);

        JSONObject res = new JSONObject();
        if (!rAppliedExperimentService.save(rAppliedExperiment)) {
            res.put("msg", "实验选择失败");
            log.info("/chooseExperiment选择");
            return Result.error(res);
        }

        res.put("msg", "添加成功");
        return Result.success(res);
    }

    /**
     * deleteAppliedExp删除已经加入的实验
     *
     * @param request experId
     * @param map     teacherId
     * @return isSuccess
     */
    @RequestMapping("/deleteAppliedExp")
    public Result<JSONObject> deleteAppliedExp(HttpServletRequest request, @RequestBody HashMap<String, String> map) {
        String token = request.getHeader("Authorization");
        RUser user = redisUtil.getUser(token);
        log.info("/deleteAppliedExp接到请求,user:" + user + " map：" + map);

        QueryWrapper<RAppliedExperiment> rAppliedExperimentQueryWrapper = new QueryWrapper<>();
        rAppliedExperimentQueryWrapper.eq("teacher_id", user.getUserId())
                .eq("exper_id", map.get("experId"));
        boolean remove = rAppliedExperimentService.remove(rAppliedExperimentQueryWrapper);
        JSONObject res = new JSONObject();
        if (!remove) {
            res.put("msg", "删除失败");
            log.error("/deleteAppliedExp删除失败,user:" + user + " map:" + map);
            return Result.error(res);
        }
        res.put("msg", "去除成功");
        log.info("/deleteAppliedExp成功删除");
        return Result.success(res);
    }
}
